package com.sky.controller.user;

import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户端订单控制器
 */
@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "用户端订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 提交订单
     * @param ordersSubmitDTO 订单提交数据
     * @return 订单提交结果
     */
    @PostMapping("/submit")
    @ApiOperation("提交订单")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户提交订单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     * @param orderId 订单ID
     * @return 支付参数
     */
    @PostMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestParam Long orderId) {
        log.info("订单支付：orderId = {}", orderId);
        OrderPaymentVO orderPaymentVO = orderService.payment(orderId);
        return Result.success(orderPaymentVO);
    }

    /**
     * 查询历史订单
     * @param page 页码
     * @param pageSize 每页大小
     * @param status 订单状态（可选）
     * @return 分页结果
     */
    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单")
    public Result<PageResult> historyOrders(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) Integer status) {
        log.info("查询历史订单：page = {}, pageSize = {}, status = {}", page, pageSize, status);
        
        // 构建查询条件
        com.sky.dto.OrdersPageQueryDTO queryDTO = new com.sky.dto.OrdersPageQueryDTO();
        queryDTO.setPage(page);
        queryDTO.setPageSize(pageSize);
        queryDTO.setStatus(status);
        
        // 设置用户ID（从当前线程中获取用户ID）
        // 这里简化处理，实际应该从请求上下文或token中获取
        Long userId = 1L; // 临时设置，后续需要改为从会话中获取
        queryDTO.setUserId(userId);
        
        PageResult pageResult = orderService.pageQueryUser(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     * @param id 订单ID
     * @return 订单详情
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> orderDetail(@PathVariable Long id) {
        log.info("查询订单详情：id = {}", id);
        OrderVO orderVO = orderService.getOrderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     * @param id 订单ID
     * @return 操作结果
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancelOrder(@PathVariable Long id) {
        log.info("用户取消订单：id = {}", id);
        orderService.userCancelById(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id 订单ID
     * @return 操作结果
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetition(@PathVariable Long id) {
        log.info("再来一单：id = {}", id);
        // TODO: 实现再来一单功能
        return Result.success();
    }

    /**
     * 催单
     * @param id 订单ID
     * @return 操作结果
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("催单")
    public Result reminder(@PathVariable Long id) {
        log.info("用户催单：id = {}", id);
        orderService.remindOrder(id);
        return Result.success();
    }
}